

// window.history.forward()
// 选择器封装
const qu = name=>{
    return document.querySelector(name);
}
const cl = name=>{
    return document.getElementsByClassName(name);
}


// 处理表单
function serializeToJson(form){
    var result={};
    var f = form.serializeArray()
    f.forEach(function(item){
        result[item.name] = item.value;
    });
    return result;
}


// 退出登录

function quitFun() {
    console.log("1123");
}


// 批量删除
var dels = new Array();
var checkbox  = cl("checkbox");
function checkboxFun (e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    if(e.target.checked==false&&dels.length>0){
        dels.splice(dels.indexOf(e.target.getAttribute("name"),1))
    }
    let index = 0;
    for (let keys of checkbox) {
        if(keys.checked==true) {
            dels[index] =keys.name;
            index++;
        }
    }
    if((index)==checkbox.length){
        checkAll.checked = true;
    }else{
        checkAll.checked = false;
    }
  
}
function checkAllFun(){
    if(checkAll.checked){
        for (let key of checkbox){
            key.checked = true;
        }
        checkboxFun();
    }else{
        for (let key of checkbox){
            key.checked = false;
            dels.splice(dels.indexOf(key.name),1)
        }
        checkboxFun();
    }
    
}
function changePage(){
    checkAll.checked=false;
    for (let key of checkbox){
        key.checked = false;
        dels.splice(dels.indexOf(key.name),1)
    }
}

// 判断登录者身份
(function(window){
    let admins = qu(".admin")
    let usersA = document.getElementById('usersA')
    let users_li = qu("#users_li")
    if(admins.getAttribute("username") =="wlgzs"){
        // users_li.style.display = "block"
        users_li.setAttribute('style', 'opcity: 1 !important;display:block;');
        // usersA.setAttribute('style', 'display: block !important');
        console.log('admin');
        // usersA.style.display = "block"
    }else{
        // users_li.style.display = "none"
        users_li.setAttribute('style', 'opcity: 0 !important;display:none !important;');
        // usersA.setAttribute('style', 'display: none !important');
        console.log('normal');
        // usersA.style.display = "none"
    }
})(window)

// 退出登陆

function openLoginOutWinFun(){
    windowDiv_loginOut.style.display = "block"
    coverLogin_div.style.display = "block"
}
function cancleLoginOutFun(){
    windowDiv_loginOut.style.display = "none"
    coverLogin_div.style.display = "none"
}
function submitLoginOutFun(){
    
    if(quit.getAttribute("flag")==1){
        location.href="../admin/login"
    }else {
        location.href="../../admin/login"
    }
   
}
//修改个人信息
var setSelf = document.getElementById('setSelf');
var shade = document.getElementById('shade');
var changeInput = document.getElementsByClassName('changeInput');

var globalSex;
try {
    setSelf.onclick = ()=>{
        shade.style.display = 'block';
        let id = shade.getAttribute('data-userID');
        axios.get(`/admin/upload/getInfoByid?id=${id}`)
        .then((data)=>{
            //将表单内填入信息
            let not = "未知";
            let info = data.data.data.object;
            changeInput[0].value = info.name;
            changeInput[1].value = info.username;
            changeInput[2].value = info.password;
            if(info.studentId && info.studentId!=="null"){
                changeInput[3].value = info.studentId;
            }else{
                changeInput[3].value = not
            }
            if(info.classId){
                 changeInput[4].value = info.classId;
            }else{
                changeInput[4].value = not
            }
    
            let sex = info.sex;
            if(sex === "null" || sex === "未知"){
                sex = "未知"
                up(3)
                down(1)
                down(2)
            }else if(sex === "男"){
                up(1)
                down(2)
                down(3)
            }else{
                up(2)
                down(1)
                down(3)
            }
            //将用户最初的信息设置为属性，判断是否更改
            changeInput[0].setAttribute('data-0',info.name)
            changeInput[1].setAttribute('data-1',info.username)
            changeInput[2].setAttribute('data-2',info.password)
            if(info.studentId && info.studentId!=="null"){
                changeInput[3].setAttribute('data-3',info.studentId)
            }else{
                changeInput[3].setAttribute('data-3',not)
            }
            if(info.classId){
                changeInput[4].setAttribute('data-4',info.classId)
           }else{
                changeInput[4].setAttribute('data-4',not)
           }
            changeInput[4].setAttribute('data-sex',sex)
            globalSex = sex;
        })
        .catch((err)=>{
            console.log(err)
        })
    }
    //确定修改个人信息
    var changeYesBtn = document.getElementById('changeYesBtn');
    var admin = document.getElementsByClassName('admin')[0];
    changeYesBtn.onclick = function(){
        console.log('asd')
        let ifchange = 0;
        for(var i = 0;i < changeInput.length;i++){
            if(changeInput[i].getAttribute(`data-${i}`) !== changeInput[i].value){
                ifchange += 1;
            }
        }
        if(changeInput[4].getAttribute('data-sex') !== globalSex){
            ifchange += 1;
        }
        console.log(ifchange)
        if(ifchange === 0){
            closeChangeFunc();
        }else{
            if(changeInput[2].value.length === 0){
                alert('密码不能为空！')
            }else if(changeInput[3].value.length !== 11){
                alert('请检查学号是否正确！')
            }else{
                axios.get(`/admin/upload/changeSelf?id=${shade.getAttribute('data-userID')}&name=${encodeURIComponent(changeInput[0].value)}&password=${changeInput[2].value}&username=${changeInput[1].value}&classId=${encodeURIComponent(changeInput[4].value)}&sex=${encodeURIComponent(globalSex)}&studentId=${changeInput[3].value}`)
                .then((data)=>{
                    console.log(data)
                    //将用户修改的信息设置为属性
                    changeInput[0].setAttribute('data-0',changeInput[0].value)
                    changeInput[1].setAttribute('data-1',changeInput[1].value)
                    changeInput[2].setAttribute('data-2',changeInput[2].value)
                    changeInput[3].setAttribute('data-3',changeInput[3].value)
                    changeInput[4].setAttribute('data-4',changeInput[4].value)
                    changeInput[4].setAttribute('data-sex',globalSex)
                    layui.use("layer",function(){
                        layer.msg('修改成功！', {
                            icon: 1,
                            time: 2000
                        })
                    }) 
                    closeChangeFunc();
                    admin.innerHTML = `您好：${changeInput[0].value}`;
                    admin.setAttribute('name',changeInput[0].value)
                })
                .catch((err)=>{
                    console.log(err)
                })
            }
        }
    }


    //关闭修改信息
    function closeChangeFunc(){
        shade.style.display = 'none';
    }


    var closeChange = document.getElementById('closeChange');
    var changeNoBtn = document.getElementById('changeNoBtn');
    closeChange.onclick = closeChangeFunc;
    changeNoBtn.onclick = closeChangeFunc;


    //选择性别
    var sexRadio = document.getElementsByClassName('sexRadio');
    var sexDiv = document.getElementsByClassName('sexDiv');
    sexDiv[0].onclick = function(){
        down(2)
        down(3)
        up(1)
        globalSex = "男"
    }
    sexDiv[1].onclick = function(){
        down(1)
        down(3)
        up(2)
        globalSex = "女"
    }
    sexDiv[2].onclick = function(){
        down(1)
        down(2)
        up(3)
        globalSex = "未知"
    }
    function down(i){
        sexRadio[i-1].className = 'layui-icon-circle layui-icon sexRadio'
    }
    function up(i){
        sexRadio[i-1].className = 'layui-icon-radio layui-icon sexRadio'
    }
} catch (TypeError) {
    console.log('berber')
}

// alert( localStorage.getItem("key"))
if (localStorage.getItem("key")==false) {
    window.location.reload()
}



// 过滤输入框标签
function htmlEscape(text){
    return text.replace(/[<>"&]/g,function(match,pos,originalText){
        switch(match){
        case "<":return "&lt";
        case ">":return "&gt";
        }
    });
}

// 打开我的面试
var my = document.getElementById('my');
var myShade = document.getElementById('myShade');
var mytbody = document.getElementById('mytbody');
my.onclick = ()=>{
    myShade.style.display = 'block';
    let name = myShade.getAttribute('name');
    axios.get(`/admin/upload/getMyInterview?user=${name}`)
    .then((data)=>{
        let info = data.data.info;
        let str = "";
        for(aa of info){
            str += `
            <tr>
                <td>${aa.name}</td>
                <td>${aa.classes}</td>
                <td>${aa.state}</td>
                <td>${aa.time}</td>
                <td>${aa.place}</td>
                <td><button class="nowBtn" stuId=${aa.studentId}>开始面试</button></td>
            </tr>
            `
            console.log(aa)
        }
        mytbody.innerHTML = str;
    })
    .catch((err)=>{
        console.log(err)
    })
}

$(mytbody).on('click','.nowBtn',(ev)=>{
    let target = ev.target || ev.srcElement;
    window.location.href=`../admin/interview?id=${target.getAttribute('stuId')}`;
})

// 关闭我的面试
document.getElementById('closeMyChange').onclick = ()=>{
    myShade.style.display = 'none';
}
// 初始化时间和地点
function initTimePlaceFun(id_){
    axios({
        url:'/admin/tables/setPlaceAndTime',
        method:'POST',
        params: {
            id: id_,
            time:"未设置",
            place:"未设置"
        }
    })
}
// 保存面试官
function saveInterviews_a(x){
    axios({
       url:"/admin/tables/changeInterviews",
       method:"POSt",
       params: {
             _id:x,
             interviews:'未设置'
       }
    })
    
 }

